package com.smart.pro;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class myMapView extends MapView {

	MyItemizedOverlay myItemizedOverlay = null;
	MyLocationOverlay myLocationOverlay = null;

	public myMapView(Context context, String apiKey) {
		super(context, apiKey);
		init(context);
	}

	public myMapView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public myMapView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context ctx){
		setClickable(true);
		setBuiltInZoomControls(true);

		Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
		int markerWidth = marker.getIntrinsicWidth();
		int markerHeight = marker.getIntrinsicHeight();
		marker.setBounds(0, markerHeight, markerWidth, 0);

		myItemizedOverlay = new MyItemizedOverlay(marker);
		getOverlays().add(myItemizedOverlay);

		myLocationOverlay = new MyLocationOverlay(ctx, this);
		getOverlays().add(myLocationOverlay);

	}

	public void addMarker(GeoPoint p, String title, String snippet){
		myItemizedOverlay.addItem(p, title, snippet);
	}

	public void callOnResume(){
		myLocationOverlay.enableMyLocation();
		myLocationOverlay.enableCompass();
	}

	public void callOnPause(){
		myLocationOverlay.disableMyLocation();
		myLocationOverlay.disableCompass();
	}

	public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

		public MyItemizedOverlay(Drawable defaultMarker) {
			//super(defaultMarker);
			super(boundCenterBottom(defaultMarker));
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return overlayItemList.get(i);
		}

		@Override
		public int size() {
			return overlayItemList.size();
		}

		public void addItem(GeoPoint p, String title, String snippet){
			OverlayItem newItem = new OverlayItem(p, title, snippet);
			overlayItemList.add(newItem);
			populate(); 
		}

	}

}
